#include "StomperLib/Objects/EditorObjectCollector.h"

namespace StomperLib {
EditorObjectCollector::EditorObjectCollector( )
{
}

EditorObjectCollector::~EditorObjectCollector( )
{
}

std::shared_ptr<std::vector<FileInfoObjectTuple> > EditorObjectCollector::CollectObjects(
  StompedReality::Utility::FileSystem* fileSystem )
{
  std::shared_ptr<std::vector<FileInfoObjectTuple> > result = std::shared_ptr<std::vector<FileInfoObjectTuple> >(
      new std::vector<FileInfoObjectTuple>);
  auto patchFiles = fileSystem->findFiles( this->GetFilter( ) );

  for (auto fileIter = patchFiles->begin( ); fileIter != patchFiles->end( ); ++fileIter)
  {
    auto jsonData = fileSystem->openAsJson( *fileIter );

    std::cout << "PF: " << fileIter->name << std::endl;
    this->ParseJson( GetContentNode( jsonData ), result, *fileIter );
  }

  return result;
}

void EditorObjectCollector::ParseJson( Json::Value dataNodes,
                                       std::shared_ptr<std::vector<FileInfoObjectTuple> > resList,
                                       StompedReality::Utility::FileInfo fileInfo )
{
  auto d =  dataNodes.size( );
  for (unsigned int i = 0; i < dataNodes.size( ); ++i)
  {
    StompedReality::type_system::TypeInfo* info =
      StompedReality::type_system::TypeSystem::GetTypeByName( dataNodes[i]["type"].asCString( ));
    if (info == NULL)
      continue;

    int s = info->size;
    StomperLib::EditorObjectInfo* obj = (StomperLib::EditorObjectInfo*) info->newPtr( );
    info->read( dataNodes[i], (char*)obj );
    FileInfoObjectTuple tpl;
    tpl.fileInfo = fileInfo;
    tpl.object = obj;
    resList->push_back( tpl );
  }
}

}